package com.wn.access.controller;

import com.wn.access.product.BankRequsetProduct;
import com.wn.access.service.ITradeLogService;
import com.wn.common.model.dto.bank.BankBidDTO;
import com.wn.common.model.dto.bank.BankCardAddDto;
import com.wn.common.model.dto.bank.BankCardRequestDto;
import com.wn.common.model.dto.bank.BorrowRecordStatusBankDTO;
import com.wn.common.model.vo.bank.SignatureVo;
import com.wn.common.model.vo.borrow.BidBalanceDTO;
import com.wn.common.model.vo.borrow.BorrowRecordVo;
import com.wn.common.model.vo.borrow.LoanVo;
import com.wn.common.model.vo.pay.RepayVo;
import com.wn.common.result.BaseResult;
import com.wn.common.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2023-07-27
 */
@Slf4j
@RestController
@RequestMapping("/trade")
public class TradeLogController {

    @Autowired
    private ITradeLogService tradeLogService;

    @Autowired
    private BankRequsetProduct bankRequsetProduct;

    @ApiOperation("托管银行绑卡的业务")
    @PostMapping("/bank/access/card")
    public Result bankCardAddDto(@RequestBody BankCardAddDto bankCardAddDto){

        //银行绑卡
        SignatureVo signatureVo =tradeLogService.bankCardAddDto(bankCardAddDto);
        return Result.ok(signatureVo);

    }

    /**
     *  绑卡成功后的数据同步问题:
     *
     *  1、 tradeLog 表的字段
     *        synchronize_status   数据同步字段
     *        CONFIRM_DATE         消息确认时间
     *
     *  2、u_user_detail      表
     *         CARD_BIND_STATUS    绑卡状态
     *
     *  3、u_user_card    表
     *         BANK_NO              银行卡编号
     *         BANK_title           银行名称
     *         STATUS               状态
     *
     *   需要更新上面的数据
     *      银行系统会给  托管平台、用户 发送数  ( 拿到数据后就进行更新 )
     */
    @ApiOperation("系统系统绑卡成功发的通知...")
    @PostMapping("/bank/access/msg")
    public String bankSendNotify(@RequestBody BankCardRequestDto bankCardRequestDto){

        //01  tradeLog 表的字段   (tradelog 数据同步 )
        tradeLogService.updateTradeLog(bankCardRequestDto);

        //02  通知给 用户端  MQ 发送到 System 微服务中

        bankRequsetProduct.sendBankMsg(bankCardRequestDto);
        log.info("银行系统数据发送到 system");

        return "通知已发出...";
    }

    @ApiOperation("借款记录同步到银行系统")
    @PostMapping("/bank/access/snyc/borrow")
    public Result syncBorrowRecord(@RequestBody BorrowRecordVo borrowRecordVo){

        log.info("trade 平台 发送数据:{}",borrowRecordVo);

        BaseResult baseResult = tradeLogService.syncBorrowRecord(borrowRecordVo);

        return Result.ok(baseResult);
    };

    @ApiOperation("机构认购投资（投标）")
    @GetMapping("/bank/access/bid/balance/{userCode}")
    public BidBalanceDTO getBalanceDetails(@PathVariable("userCode") String userCode){
        BidBalanceDTO bidBalanceDTO = tradeLogService.getBalanceDetails(userCode);
        log.info("拿到数据 返回远程调用的数据===>:{}",bidBalanceDTO);
        return bidBalanceDTO;
    }

    @PostMapping("/bank/access/add/agencyBid")
    Result<BaseResult> saveAgencyBid(@RequestBody BankBidDTO bankBidDTO){
        log.info("trade平台 发送数据:{}",bankBidDTO);

        BaseResult baseResult = tradeLogService.saveAgencyBid(bankBidDTO);

        return Result.ok(baseResult);
    }

    @PostMapping("/bank/access/loan/confirm")
    Result<BaseResult> confirmLoan(@RequestBody LoanVo loanVo){
        log.info("trade平台 发送数据确认放款数据:{}",loanVo);
        BaseResult baseResult = tradeLogService.confirmLoan(loanVo);
        log.info("拿到数据 返回远程调用的数据===>:{}",baseResult);
        return Result.ok(baseResult);
    }

    @ApiOperation(value = "确认还款")
    @PostMapping("/bank/access/confirm/repay")
    public Result<BaseResult> confirmRepay(
            @RequestBody RepayVo repaymentRequest) {
        BaseResult baseResult = tradeLogService.confirmRepay(repaymentRequest);
        log.info("拿到数据 返回远程调用的数据===>:{}",baseResult);
        return Result.ok(baseResult);
    }

    @ApiOperation(value = "修改借款的状态")
    @PostMapping("/bank/access/status")
    Result changeBorrowStatus(@RequestBody BorrowRecordStatusBankDTO borrowRecordStatusBankDTO){
        Result result = tradeLogService.changeBorrowStatus(borrowRecordStatusBankDTO);
        log.info("拿到数据 返回远程调用的数据===>:{}",result);
        return Result.ok(result);
    }
}
